<?php

namespace app\api\controller;

use app\api\model\page\Page as AppPage;
use app\api\model\settings\Setting as SettingModel;
use app\api\model\examination\Log as ExaminationLogModel;
use app\api\model\examination\ExaminationAward as ExaminationAwardModel;
use app\api\model\examination\ExaminationUserAward as ExaminationUserAwardModel;
/**
 * 页面控制器
 */
class Index extends Controller
{
    /**
     * 首页
     */
    public function index($page_id = null, $url = '')
    {
        // 页面元素
        $data = AppPage::getPageData($this->getUser(false), $page_id);
        $data['setting'] = array(
            'collection' => SettingModel::getItem('collection'),
            'officia' => SettingModel::getItem('officia'),
            'homepush' => SettingModel::getItem('homepush'),
        );
        // 扫一扫参数
        $data['signPackage'] = $this->getScanParams($url)['signPackage'];
        return $this->renderSuccess('', $data);
    }

    public function settlement()
    {
        $time=time()+100;
        $config = SettingModel::getItem('rankingsettlement');
        $arr=getEndtime($config['status']);
        $result=array_merge($config,$arr);
        if($time>=strtotime($result['time'])){
            $ExaminationUserAwardModel=new ExaminationUserAwardModel();
            //发放奖励
            $ExaminationAwardModel=new ExaminationAwardModel();
            $Award=$ExaminationAwardModel->getList();
            if(!empty($Award)){
                $ExaminationLogModel=new ExaminationLogModel();
                $log=$ExaminationLogModel->getLlistarr($ExaminationLogModel);
                if(!empty($log)){
                    foreach ($Award as $k=>$v){
                        if(!empty($log[$v['ranking']-1])){
                            $arr=['user_id'=>$log[$v['ranking']-1]['user_id'],'award_id'=>$v['id'],'examination_log_id'=>$log[$v['ranking']-1]['id'],'app_id'=>$this->app_id];
                            $ExaminationUserAwardModel->save($arr);
                        }
                    }
                }

            }

        }
        return $this->renderSuccess('成功');
    }


    /**
     * 首页
     */
    public function diy($page_id = null, $url = '')
    {
        // 页面元素
        $data = AppPage::getPageData($this->getUser(false), $page_id);
        // 微信公众号分享参数
        $data['share'] = $this->getShareParams($url, $data['page']['params']['share_title'], $data['page']['params']['share_title'], '/pages/diy-page/diy-page', $data['page']['style']['toplogo']);
        return $this->renderSuccess('', $data);
    }

}
